﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LevelGeneration : MonoBehaviour
{
    [Header("中心点探测器")]
    private Transform parent;
    [Header("人物位置")]
    private Transform player;
    [Header("关卡生成上下边界")]
    public float Range;
    public float up;
    public float down;

    public bool haveCreated = false;

    private LevelMove levelController;

    // Start is called before the first frame update
    void Awake()
    {
        levelController = GameObject.FindGameObjectWithTag("Level").GetComponent<LevelMove>();
        parent =GameObject.FindGameObjectWithTag("Level").GetComponent<Transform>();
        player = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
    }

    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        float up = player.position.y + Range;
        float down = player.position.y - Range;
        float y = parent.TransformPoint(transform.localPosition).y;
        // 关卡移出相机范围后删除
        if (y >= up)
        {
            Destroy(this.gameObject);
            Debug.Log("Destroy");
        }
        // 摄像头移至下侧边界后产生新的关卡
        else if (y >= down && !haveCreated)
        {
            levelController.OnCreateLevel();
            haveCreated = true;
            Debug.Log("Add");
        }
        //print("人物坐标"+player.position+"up:"+up+"down:"+down);
    }

}
